package com.sg.service.biz.component.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.exception.BizException;
import com.sg.dto.biz.component.common.DataSetsListTwoDto;
import com.sg.dto.biz.component.common.DocumentDto;
import com.sg.dto.biz.component.req.ImplementDocDataSetMergeFieldReqDto;
import com.sg.dto.biz.component.req.ImplementFileClearReqDto;
import com.sg.dto.biz.component.req.ImplementTenReceivingFieldReqDto;
import com.sg.dto.biz.component.req.ObtainReceiveFieldsReqDto;
import com.sg.dto.biz.component.res.ImplementDocDataSetMergeFieldRespDto;
import com.sg.dto.biz.component.res.ImplementFileClearRespDto;
import com.sg.dto.biz.component.res.ImplementTenReceivingFieldRespDto;
import com.sg.dto.biz.component.res.ObtainReceiveFieldsRespDto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbUnstructuredData {
    /**
     * code:receptionService
     * name:M2-获取接收字段
     * desc:undefined
     * gen by moon at 9/18/2022, 8:14:08 PM
     **/
    @Trace(operationName = "M2-获取接收字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveFieldsRespDto obtainReceiveFields(ObtainReceiveFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveFieldsRespDto.class);
    }

    @Trace(operationName = "执行磁盘删除")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public void excuteDelFile(String filePath) {


        // 创建一个File对象
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }

        // 调用delete方法尝试删除文件
        boolean result = file.delete();

        // 检查文件是否成功被删除
        if (result) {

        } else {
            throw new BizException("-1", "文件删除失败", false);
        }
    }

    /**
     * code:clearFile
     * name:M2执行文件清理(特殊方法）
     * desc:undefined
     * gen by moon at 1/25/2024, 9:46:36 PM
     **/
    @Trace(operationName = "M2执行文件清理(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFileClearRespDto implementFileClear(ImplementFileClearReqDto reqDto) {
        // TODO ruizhe skai dong ; 1/25/2024, 9:46:36 PM
        String rootPath = reqDto.getFileStorageRootPath();
        String relPath = reqDto.getFileRelativePathAccess();
        if (reqDto.getFileStorageRootPath() != null && reqDto.getFileStorageRootPath().endsWith("/")) {
            rootPath = rootPath.substring(0, rootPath.length() - 1);
        }

        if (reqDto.getFileRelativePathAccess() != null && !reqDto.getFileRelativePathAccess().startsWith("/")) {
            relPath = "/" + relPath;
        }

        if (rootPath == null || rootPath.isEmpty() || relPath == null || relPath.isEmpty()) {
            throw new BizException("-1", "文件根路径或者文件相对路径为空", false);
        }
        String targetFilePath = rootPath + relPath;
        excuteDelFile(targetFilePath);
        return new ImplementFileClearRespDto();
    }

    /**
     * code:receptionService
     * name:M2-接收文件夹 ID
     * desc:undefined
     * gen by moon at 3/27/2024, 1:00:17 AM
     **/
    @Trace(operationName = "M2-接收文件夹 ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementTenReceivingFieldRespDto implementTenReceivingField(ImplementTenReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementTenReceivingFieldRespDto.class);
    }

    /**
     * code:docDataSetMergeField
     * name:M2执行文档数据集交集合并字段（特殊方法）
     * desc:undefined
     * gen by moon at 3/27/2024, 1:02:51 AM
     **/
    @Trace(operationName = "M2执行文档数据集交集合并字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementDocDataSetMergeFieldRespDto implementDocDataSetMergeField(ImplementDocDataSetMergeFieldReqDto reqDto) {
        ImplementDocDataSetMergeFieldRespDto retData = new ImplementDocDataSetMergeFieldRespDto();
        for (DataSetsListTwoDto one : reqDto.getDataSetsListTwo()) {
            if (reqDto.getDataSetsListOne().contains(one.getUnstructuredDataId())) {
                DocumentDto elm = new DocumentDto();
                elm.setUnstructuredDataId(one.getUnstructuredDataId());
                elm.setSourceRelationshipId(one.getSourceRelationshipId());
                elm.setRelationshipType(one.getRelationshipType());
                elm.setIsNetworkReference(one.getIsNetworkReference());
                elm.setPostfix(one.getPostfix());
                elm.setFileRelativePathAccess(one.getFileRelativePathAccess());
                retData.getDocumentList().add(elm);
            }
        }
        return retData;
    }
    //手工接入方法
}
